home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack'90 Proceedings / John Norstad / Reusable Code / Source / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  5.2 KB  |  232 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     init.c - Initialization Module
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is 
  6.     granted to use this code in your own projects, provided you give credit 
  7.     to both John Norstad and Northwestern University in your about box or 
  8.     document.
  9.     
  10.     This module takes care of program intialization.
  11. _____________________________________________________________________*/
  12.  
  13.  
  14. #pragma load "precompile"
  15. #include "utl.h"
  16. #include "rez.h"
  17. #include "glob.h"
  18. #include "misc.h"
  19. #include "main.h"
  20. #include "pref.h"
  21. #include "help.h"
  22. #include "abou.h"
  23. #include "init.h"
  24.  
  25. #pragma segment init
  26.  
  27. /*______________________________________________________________________
  28.  
  29.     MyGrowZone - Grow Zone Procedure.
  30.     
  31.     Entry:    cbNeeded = number of bytes needed.
  32. _____________________________________________________________________*/
  33.  
  34.  
  35. #pragma segment Main
  36.  
  37. static pascal long MyGrowZone (Size cbNeeded)
  38.  
  39. {
  40. #pragma unused (cbNeeded)
  41.  
  42.     if (NoMemFullAlert) return 0;
  43.     misc_Notify(requiresAttn, true);
  44.     (void) utl_StopAlert(noMemID, nil, 0);
  45.     ExitToShell();
  46. };
  47.  
  48. #pragma segment init
  49.  
  50. /*______________________________________________________________________
  51.  
  52.     init_InitMem - Initialize Memory.
  53. _____________________________________________________________________*/
  54.  
  55.  
  56. #pragma segment Main
  57.  
  58. void init_InitMem (void)
  59.  
  60. {
  61.     /* Increase stack space by 20,000 bytes */
  62.     
  63.     SetApplLimit(GetApplLimit() - 20000);
  64.     
  65.     /* Expand the application heap zone to the maximum. */
  66.     
  67.     MaxApplZone();
  68.     
  69.     /* Allocate a bunch of master pointers. */
  70.     
  71.     MoreMasters();
  72.     MoreMasters();
  73.     MoreMasters();
  74.     MoreMasters();
  75. };
  76.  
  77. #pragma segment init
  78.  
  79. /*______________________________________________________________________
  80.  
  81.     init_Initialize - Initialize the Program.
  82.     
  83.     Ths routine is put in its own segment, which is unloaded after
  84.     it's used.
  85. _____________________________________________________________________*/
  86.  
  87.  
  88. void init_Initialize (void)
  89.  
  90. {
  91.     Handle            rectHandle;            /* handle to rectangle list */
  92.     short                i;                        /* loop index */
  93.     FCBPBRec            fcbBlock;            /* file info param block */
  94.     EventRecord        event;                /* event record */
  95.     
  96.     /* Initialize all the managers. */
  97.  
  98.     InitGraf(&qd.thePort);
  99.     InitFonts();
  100.     FlushEvents(everyEvent, 0);
  101.     InitWindows();
  102.     InitMenus();
  103.     TEInit();
  104.     InitDialogs(nil);
  105.     
  106.     /* Establish a grow zone procedure to catch out of memory conditions. */ 
  107.     
  108.     CouldAlert(noMemID);
  109.     SetGrowZone(MyGrowZone);
  110.     
  111.     /* Call WaitNextEvent a few times to force our window to initially appear
  112.         in the foreground.  See TN 180. */
  113.         
  114.     utl_WaitNextEvent(everyEvent, &event, 0, nil);
  115.     utl_WaitNextEvent(everyEvent, &event, 0, nil);
  116.     
  117.     /* Check to make sure we have HFS. */
  118.     
  119.     if (*(short*)FSFCBLen <= 0) {
  120.         (void) utl_StopAlert(noHFSID, nil, 0);
  121.         ExitToShell();
  122.     };
  123.     
  124.     /* Get the ROM environment. */
  125.     
  126.     OldRom = utl_Rom64();
  127.     
  128.     /* Get the file ref nums of the system file and Disinfectant. */
  129.     
  130.     SysRefNum = *((short*)SysMap);
  131.     DfectRefNum = *((short*)CurMap);
  132.     DfectVol = utl_GetApplVol();
  133.     
  134.     /* Get the system volume reference number and the directory id of
  135.         the blessed folder. */
  136.         
  137.     SysVol = utl_GetSysVol();
  138.     if (!utl_VolIsMFS(SysVol)) {
  139.         fcbBlock.ioNamePtr = nil;
  140.         fcbBlock.ioVRefNum = 0;
  141.         fcbBlock.ioRefNum = SysRefNum;
  142.         fcbBlock.ioFCBIndx = 0;
  143.         (void) PBGetFCBInfo(&fcbBlock, false);
  144.         SysDirID = fcbBlock.ioFCBParID;
  145.     };
  146.     
  147.     /* Check to make sure all the packages we need exist.  If they don't 
  148.         exist, put up an alert telling the user we require system 3.2 or 
  149.         later and exit to shell. */
  150.         
  151.     if (!utl_CheckPack(listMgr, false) || !utl_CheckPack(stdFile, false) || 
  152.         !utl_CheckPack(dskInit, false) || !utl_CheckPack(intUtil, false) || 
  153.         !utl_CheckPack(bdConv, false)) {
  154.         (void) utl_StopAlert(badSysID, nil, 0);
  155.         ExitToShell();
  156.     };
  157.     
  158.     /* Determine whether or not the system has the shutdown trap.
  159.         (See TN 156).*/
  160.     
  161.     SysHasShutDown = NGetTrapAddress(_ShutDown, ToolTrap) !=
  162.         NGetTrapAddress(_Unimplemented, ToolTrap);
  163.         
  164.     /* Get the long sleep time. */
  165.     
  166.     LongSleep = utl_GetLongSleep();
  167.     
  168.     /* Get the watch cursor. */
  169.     
  170.     Watch = GetCursor(watchCursor);
  171.     
  172.     /* Get the beachball cursors. */
  173.     
  174.     for (i = firstBallID; i < firstBallID + numBalls; i++) {
  175.         BBCursors[i-firstBallID] = GetCursor(i);
  176.     };
  177.     
  178.     /* Get the help cursor and the floppy disk cursor. */
  179.     
  180.     HelpCurs = GetCursor(helpCursID);
  181.     FloppyCurs = GetCursor(floppyCursID);
  182.     
  183.     /* Initialize the menus. */
  184.  
  185.     SetMenuBar(GetNewMBar(mBarID));
  186.     AddResMenu(GetMHandle(appleMID), (ResType) 'DRVR');
  187.     DrawMenuBar();
  188.     
  189.     /* Enable fractional character widths for better looking 
  190.         LaserWriter output. */
  191.     
  192.     SetFractEnable(true);
  193.     
  194.     /* Initialize the drag rectangle. */
  195.     
  196.     DragRect = (***(RgnHandle*)GrayRgn).rgnBBox;
  197.     InsetRect(&DragRect, dragSlop, dragSlop);
  198.     
  199.     /* Get the rectangle list. */
  200.     
  201.     rectHandle = GetResource(rectType, rectID);
  202.     (void) memcpy(RectList, *rectHandle, sizeof(Rect)*numRects);
  203.     ReleaseResource(rectHandle);
  204.     
  205.     /* Initialize the preferences. */
  206.     
  207.     misc_ReadPref();
  208.     
  209.     /* Reopen windows, in back-to-front order. */
  210.     
  211.     for (i = Prefs.numOpenWind-1; i >= 0; i--) {
  212.         switch (Prefs.openWind[i]) {
  213.             case mainWind:
  214.                 main_Open();
  215.                 break;
  216.             case helpWind:
  217.                 help_Open(0);
  218.                 break;
  219.             case prefWind:
  220.                 pref_Open();
  221.                 break;
  222.             case abouWind:
  223.                 abou_Open();
  224.                 break;
  225.         };
  226.     };
  227.     
  228.     /* Initialize the cursor. */
  229.  
  230.     InitCursor();
  231. }
  232.